package com.example.myapplication.tack_24;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_24.adapter.OrderAdapter;
import com.example.myapplication.tack_24.data.OrderData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

public class OrderActivity extends AppCompatActivity {
    private ImageView exit;
    private ImageView img;
    private TextView infoNo;
    private TextView complaint;
    private RecyclerView recycler;
    private OrderData orderData;
    private Gson gson;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_24_activity_order);
        init();
        exit_();
        getOrder();
        complaint_();
    }

    private void complaint_() {
        complaint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderActivity.this,ComplaintActivity.class);
                String json = gson.toJson(orderData);
                intent.putExtra(Common.COMMON,json);
                OrderActivity.this.startActivity(intent);
            }
        });
    }

    private void getOrder() {
        String url = "prod-api/api/logistics-inquiry/logistics_info/ST0000001";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, OrderData.class, new OkHttpTool.NetCallback() {

            @Override
            public void onSuccess(Object obj) {
                orderData = (OrderData)obj;
                if(orderData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(OrderActivity.this).load(OkHttpTool.URL+ orderData.getData().getCompany().getImgUrl()).into(img);
                            infoNo.setText(orderData.getData().getInfoNo());
                            recycler.setLayoutManager(new LinearLayoutManager(OrderActivity.this));
                            recycler.setAdapter(new OrderAdapter(orderData.getData().getStepList(),OrderActivity.this));
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderActivity.this.finish();
            }
        });
    }

    private void init() {
        gson = new Gson();
        exit = findViewById(R.id.exit);
        img = findViewById(R.id.img);
        infoNo = findViewById(R.id.infoNo);
        complaint = findViewById(R.id.complaint);
        recycler = findViewById(R.id.recycler);
    }
}